<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流水线详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            帮助中心:<a href="#" target="_blank">帮助中心</a>
            <a class="layui-btn" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
              <i class="layui-icon">&#xe669;</i></a>
        </blockquote>
        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>       
    </div>
    	<!-- 弹添加框-->
  	<div id="AddRuleInfo" style="display:none;margin: 40px 40px 40px 40px">
        <blockquote class="layui-elem-quote layui-text">
            帮助中心:<a href="#" target="_blank">添加说明</a>
        </blockquote>
          <form class="layui-form layui-form-pane" action=""  lay-filter="UpdateInfo">
              <div class="layui-form-item">
                  <input type="hidden" name="id" value="" class="layui-input">
                  <div class="layui-inline">
                      <label class="layui-form-label">aliyunId</label>
                      <div class="layui-input-inline"  style="width:250px">
                          <input type="text" name="aliyun_id" autocomplete="off" value=""  placeholder="" class="layui-input">
                      </div>
                  </div>
                  <div class="layui-inline">
                      <label class="layui-form-label">accesskeyId</label>
                      <div class="layui-input-inline" style="width:250px">
                          <input type="text" name="accesskey_id" id="accesskey_id" autocomplete="off" value=""  placeholder="" class="layui-input">
                      </div>
                  </div>
                  <div class="layui-inline">
                      <label class="layui-form-label">accesskeySecret</label>
                      <div class="layui-input-inline" style="width:250px">
                          <input type="text" name="accesskey_secret" id="accesskey_secret" autocomplete="off" value=""  placeholder="" class="layui-input">
                      </div>
                  </div>

                  <div class="layui-inline">
                      <label class="layui-form-label">组织ID</label>
                      <div class="layui-input-inline" style="width:250px">
                          <input type="text" name="organization_id" id="organization_id" autocomplete="off" value=""  placeholder="" class="layui-input">
                      </div>
                      <input type="button" class="layui-btn" value="验证AK并获取组织ID" onclick="GetOrganizationId()"></input>
                  </div>

                  <div class="layui-inline">
                      <label class="layui-form-label">备注</label>
                      <div class="layui-input-inline" style="width:250px">
                          <input type="text" name="remarks" autocomplete="off" class="layui-input">
                      </div>
                  </div>
              </div>
              <div class="layui-form-item">
                  <div class="layui-inline" style="margin-left:120px">
                      <button type="submit" class="layui-btn"  lay-submit lay-filter="Add"><i class="layui-icon">&#xe61f;</i>确认提交</button>
                  </div>
              </div>
          </form>
	  <!-- 弹添加框end-->       
    
    </div>
</div>
</body>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="Add"><i class="layui-icon">&#xe61f;</i>添加阿里云帐号</button>
            </div>
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script> 

<script>

function GetOrganizationId() {
    var obj= {
      "accesskey_id":$('#accesskey_id').val(),
      "accesskey_secret":$('#accesskey_secret').val(),
    };

    console.log(JSON.stringify(obj));
    $.ajax({
      url: "/cicd/v1/GetOrganizationsByAk",
      type: "POST",
      data: JSON.stringify(obj),
      dataType: "json",
      success: function (resp) {
            //console.log(resp);
            if(resp.success == true){
                document.getElementById('organization_id').value = JSON.stringify(resp.organizations);
                layer.msg('成功', {icon: 1});
            }else{
                layer.msg(resp.msg,{icon:2});
            }
      }
    });	
    return false
}

    layui.use(['form', 'table','miniTab','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            table = layui.table;
            miniTab = layui.miniTab,
            miniTab.listen();

        table.render({
            elem: '#currentTableId',
            url: '/cicd/v1/AkList',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                {field: 'id',width:60,  title: 'ID', sort: true},
                {field: 'aliyun_id',title: 'aliyunId'},
                {field: 'accesskey_id',title: 'accesskeyId'},
                {field: 'accesskey_secret',hide:true, title: 'accesskeySecret'},
                {field: 'organization_id',width:200,title: 'organizationId', sort: true},
                {field: 'remarks',title: '备注',edit:'text'},
                {field: 'createtime', title: '创建时间', hide:true},
                {title: '操作', width:150,toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 15,
            page: true
        });

  	    table.on('toolbar(currentTableFilter)', function(obj){
    	    //var checkStatus = table.checkStatus(obj.config.id);
        	switch(obj.event){
          		case 'Add':
    					layer.open({
                			type:1,
    						      skin: 'layui-layer-rim',
                			title:"添加阿里云帐号",
                			area: ['80%', '80%'],
                			content:$("#AddRuleInfo")
            	});
              form.render();	
          		break;									
        	};
  	    });

        form.on('submit(Add)', function (data) {
                data.field.aliyun_id = data.field.aliyun_id.replace(/^\s*|\s*$/g,""); //替换空格
                data.field.accesskey_id = data.field.accesskey_id.replace(/^\s*|\s*$/g,"");
                data.field.accesskey_secret = data.field.accesskey_secret.replace(/^\s*|\s*$/g,""); 
                //data.field.organization_id = data.field.organization_id.replace(/^\s*|\s*$/g,"");          
					      //console.log(data.field);
                data.field.id = Number(data.field.id);
			          layer.confirm('确定?', {icon: 3, title:'提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行3-5s左右');
                     $.ajax({
                        url: "/cicd/v1/AkAdd" + location.search,
                        type: "POST",
                        data: JSON.stringify(data.field),
                        dataType: "json",
                        success: function (resp) {
                              layer.close(index2);
                              if(resp.code == 0){
                                  layer.msg('成功', {icon: 1});
                              }else{
                                  layer.msg(resp.msg,{icon:2});
                              }
                        }
                      });		  	  
                  },
                  cancel: function(index, layero){ 
                    layer.close(index);
                    layer.close(index2);
		                console.log("不操作");
                  } 
                });
              return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('确定删除'+data.id+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('请稍等...');
                			 $.ajax({
                			    url: "/cicd/v1/AkDel?id="+data.id+"&aliyunId="+data.aliyun_id,
                			    type: "get",
                			    success: function (resp) {
                            layer.close(index2);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                							 obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            } else if (obj.event === 'edit'){
    				  form.val('UpdateInfo', {
    				    "id": data.id
                ,"aliyun_id": data.aliyun_id
              	,"accesskey_id": data.accesskey_id
              	,"accesskey_secret": data.accesskey_secret
              	,"organization_id": data.organization_id
              	,"remarks": data.remarks
    				  });  
    					layer.open({
                			type:1,
    						      skin: 'layui-layer-rim',
                			title:"编辑阿里云帐号",
                			area: ['80%', '80%'],
                			content:$("#AddRuleInfo")
            	});
              form.render();	
            }
        });

    });

</script>
</html>